package com.example.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.result.Result;
import com.example.common.result.YiFeiException;
import com.example.entity.hz.MOCTA;
import com.example.entity.hz.Printmodule;
import com.example.entity.hz.Printtemplate;
import com.example.mapper.PrinttemplateMapper;
import com.example.service.PrintmoduleService;
import com.example.mapper.PrintmoduleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 14501
* @description 针对表【Printmodule】的数据库操作Service实现
* @createDate 2024-09-03 11:16:28
*/
@Service
public class PrintmoduleServiceImpl extends ServiceImpl<PrintmoduleMapper, Printmodule> implements PrintmoduleService{

    //打印模块mapper层
    @Resource
    private PrintmoduleMapper printmoduleMapper;

    //打印模版mapper层
    @Resource
    private PrinttemplateMapper printtemplateMapper;

    @Override
    public Result queryPrintModuleIPage(Integer current, Integer size) {
        try{
            Map<String,Object> map = new HashMap<>();
            PageHelper.startPage(current,size);
            List<Printmodule> printmoduleList = this.printmoduleMapper.selectList(null);
            PageInfo<Printmodule> printmodulePageInfo = new PageInfo<>(printmoduleList);
            map.put("pages", printmodulePageInfo.getPages());
            map.put("total", printmodulePageInfo.getTotal());
            map.put("list", printmodulePageInfo.getList());
            return Result.success(map);
        }catch (Exception e){
            throw new YiFeiException(500,"查询打印模块分页发生异常"+e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result savePrintModule(List<Printmodule> printmoduleList) {
        try{
            if (!printmoduleList.isEmpty()){
                printmoduleList.forEach(arr -> {
                    arr.setCreationtime(DateUtil.format(new Date(), "yyyyMMdd"));
                    arr.setStatus("开启");
                    this.printmoduleMapper.insert(arr);
                });
            }
            return Result.success();
        }catch (Exception e){
            throw new YiFeiException(500, "新增发生异常"+e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result deletePrintModule(List<Integer> uuidList) {
        try {
            if (!uuidList.isEmpty()){
                uuidList.forEach(arr -> {
                    int i = this.printmoduleMapper.deleteById(arr);
                    if (i>0){
                        this.printtemplateMapper.delete(new LambdaQueryWrapper<Printtemplate>().eq(Printtemplate::getUuid, arr));
                    }
                });
                return Result.success();
            }
            return Result.error("请输入正确参数");
        }catch (Exception e){
            throw new YiFeiException(500, "打印模块删除发生异常"+e.getMessage());
        }
    }

    @Override
    public Result<Printmodule> queryOnePrintTemplate(Integer uuid) {
        try {
            Printmodule printmodule = this.printmoduleMapper.selectById(uuid);
            return Result.success(printmodule);
        }catch (Exception e){
            throw new YiFeiException(500, "通过uuid查询打印模块详细信息发生异常"+e.getMessage());
        }
    }

    @Override
    public Result updatePrintModule(Printmodule printmodule) {
        try{
            printmodule.setUpdatetime(DateUtil.format(new Date(), "yyyyMMdd"));
            int update = this.printmoduleMapper.updateById(printmodule);
            if (update>0){
                return Result.success();
            }
            return Result.error("修改失败!请检验传参uuid");
        }catch (Exception e){
            throw new YiFeiException(500,"打印模块修改发生异常"+e.getMessage());
        }
    }

    @Override
    public Result<List<Printtemplate>> queryTest(Integer uuid) {
        try {
            List<Printtemplate> printtemplateList = this.printtemplateMapper.selectList(null);
            return Result.success(printtemplateList);
        }catch (Exception e){
            throw new YiFeiException(500, "通过uuid查询打印模版发生异常"+e.getMessage());
        }
    }
}




